﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using backend.Models;

namespace backend.Data;

public partial class CampusFacilityRepairManagementDbContext : DbContext
{
    public CampusFacilityRepairManagementDbContext()
    {
    }

    public CampusFacilityRepairManagementDbContext(DbContextOptions<CampusFacilityRepairManagementDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Failure> Failures { get; set; }

    public virtual DbSet<FailureAreaStatistic> FailureAreaStatistics { get; set; }

    public virtual DbSet<FailureDatum> FailureData { get; set; }

    public virtual DbSet<FailureTypeStatistic> FailureTypeStatistics { get; set; }

    public virtual DbSet<FirstClass> FirstClasses { get; set; }

    public virtual DbSet<MaintenanceMan> MaintenanceMen { get; set; }

    public virtual DbSet<Profession> Professions { get; set; }

    public virtual DbSet<RepairDatum> RepairData { get; set; }

    public virtual DbSet<RepairRequest> RepairRequests { get; set; }

    public virtual DbSet<SecondClass> SecondClasses { get; set; }

    public virtual DbSet<User> Users { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseSqlServer("Name=ConnectionStrings:DefaultConnection");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Failure>(entity =>
        {
            entity.HasKey(e => e.FId).HasName("PK__Failures__2C6EC7C32F2CE28E");

            entity.HasOne(d => d.PIdNavigation).WithMany(p => p.Failures)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__Failures__P_ID__534D60F1");
        });

        modelBuilder.Entity<FailureAreaStatistic>(entity =>
        {
            entity.ToView("FailureAreaStatistics");
        });

        modelBuilder.Entity<FailureDatum>(entity =>
        {
            entity.HasKey(e => e.FdId).HasName("PK__FailureD__5842694FED2FF8E3");

            entity.Property(e => e.FdTimes).HasDefaultValue(1);

            entity.HasOne(d => d.FIdNavigation).WithMany(p => p.FailureData)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__FailureDat__F_ID__6383C8BA");

            entity.HasOne(d => d.Fc).WithMany(p => p.FailureData)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__FailureDa__FC_ID__6477ECF3");
        });

        modelBuilder.Entity<FailureTypeStatistic>(entity =>
        {
            entity.ToView("FailureTypeStatistics");
        });

        modelBuilder.Entity<FirstClass>(entity =>
        {
            entity.HasKey(e => e.FcId).HasName("PK__FirstCla__20E5419236F50FB1");
        });

        modelBuilder.Entity<MaintenanceMan>(entity =>
        {
            entity.HasKey(e => e.MId).HasName("PK__Maintena__18B1A31772BACB06");

            entity.HasOne(d => d.MIdNavigation).WithOne(p => p.MaintenanceMan)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__Maintenanc__M_ID__4D94879B");
        });

        modelBuilder.Entity<Profession>(entity =>
        {
            entity.HasKey(e => e.PId).HasName("PK__Professi__A3420A7799AFB392");
            entity.ToTable(tb => tb.HasTrigger("trg_UpdateToDefaultProfession"));
        });

        modelBuilder.Entity<RepairDatum>(entity =>
        {
            entity.HasKey(e => e.RdId).HasName("PK__RepairDa__5B49A6A386108448");

            entity.Property(e => e.RdRequestTimes).HasDefaultValue(1);
        });

        modelBuilder.Entity<RepairRequest>(entity =>
        {
            entity.HasKey(e => e.RId).HasName("PK__RepairRequests");

            entity.ToTable(tb =>
                {
                    tb.HasTrigger("trg_InsertRepairData");
                    tb.HasTrigger("trg_UpdateRepairDataRatings");
                    tb.HasTrigger("trg_UpdateStatsOnCompletion");
                });

            entity.HasOne(d => d.FIdNavigation).WithMany(p => p.RepairRequests)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__RepairRequ__F_ID__6A30C649");

            entity.HasOne(d => d.Fc).WithMany(p => p.RepairRequests)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__RepairReq__FC_ID__68487DD7");

            entity.HasOne(d => d.MIdNavigation).WithMany(p => p.RepairRequests).HasConstraintName("FK__RepairRequ__M_ID__693CA210");

            entity.HasOne(d => d.UIdNavigation).WithMany(p => p.RepairRequests)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__RepairRequ__U_ID__6754599E");
        });

        modelBuilder.Entity<SecondClass>(entity =>
        {
            entity.HasKey(e => e.ScId).HasName("PK__SecondCl__C402E49E61A349D0");

            entity.HasOne(d => d.Fc).WithMany(p => p.SecondClasses)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__SecondCla__FC_ID__59063A47");
        });

        modelBuilder.Entity<User>(entity =>
        {
            entity.HasKey(e => e.UId).HasName("PK__Users__5A2040DB3819DF1B");

            entity.Property(e => e.UPassword).IsFixedLength();
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
